<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>掉线处理</title>
    <link rel="stylesheet" href="../css/bootstrap.min.css">
    <script src="./../js/index.js"></script>
    <style>
        table {
            word-break: break-all;
            word-wrap: break-word;
            table-layout: auto;
        }

        div {
            font-size: 1rem;
        }

        td {
            width: 10rem;
        }
    </style>
</head>

<body>
    <div class="container">
        
    </div>

    <div class="container-sm">
        <h5>处理掉线数据</h5>
        <div class="mb-3 mt-3">
            <label for="totalNum" class="form-label">车辆总数:</label>
            <input type="email" class="form-control" id="totalNum" placeholder="输入总车辆数" name="totalNum">
            <span id="carNum" style="font-size: 20px;color: #f20d0d;">总车辆为空或非数字</span>
        </div>

        <!-- 请输入总车辆：<input type="text" id="totalNum"> -->
        <div class="form-group">
            <textarea class="form-control" rows="8" id="inputinfo" placeholder="粘贴车牌、最后在线时间、比对时间"></textarea>
        </div>

        <div class="form-group">
            <label class="form-label">上线情况:</label>
            <textarea class="form-control" onclick="copyText(1)" rows="6" id="OnlineSituation"
                placeholder="上线情况"></textarea><span id="cp1" style="font-size: 40px;color: #f20d0d;">复制成功！</span>
        </div>
        <label class="form-label">监控建议:</label>
        <div class="form-group">
            <textarea class="form-control" onclick="copyText(2)" rows="8" id="strf" placeholder="监控建议"></textarea>
        </div>
        <span id="cp2" style="font-size: 40px;color: #f20d0d;">复制成功！</span>

    </div>


    <script>

        var cp1 = document.getElementById("cp1"); //
        cp1.style.display = "none";

        var cp2 = document.getElementById("cp2"); //
        cp2.style.display = "none";

        var carNum = document.getElementById("carNum"); //
        carNum.style.display = "none";

        var num1 = 0
        var num2 = 0
        var num3 = 0

        // setInterval(() => {
        //     console.log("测试定时任务-------------")
        // }, 500);


        function joint() {
            num1 = 0
            num2 = 0
            num3 = 0

            var carNum = document.getElementById("carNum"); //
            carNum.style.display = "none";

            var totalNum = document.getElementById("totalNum").value
            if(totalNum.trim()==''){
                //var carNum = document.getElementById("carNum"); //
                //carNum.style.display = "block";
                return
            }
            if(isNaN(parseInt(totalNum))){
               // var carNum = document.getElementById("carNum"); //
                //carNum.style.display = "block";
                return
            }

            var inputinfo = document.getElementById("inputinfo").value

            console.log(inputinfo)
            if (inputinfo.trim() == '') {

                return
            }

            if (inputinfo.indexOf('粤') !== -1) {

            } else {
                console.log("不包含车牌-------------")

            }
            

            var strArray = inputinfo.split(/\r\n|\n/)
            // console.log(strArray)


            var result = ""
            var result1 = ""
            var result2 = ""
            var result3 = ""

            for (var i = 0; i < strArray.length; i++) {

                if (strArray[i].trim() != "") {
                    var array = strArray[i].split('\t')
                    if(array.length!=3){
                        return
                    }

                    if(array[0].trim()==''){
                        return
                    }

                    if (array[0].indexOf('粤') == -1){
                        return
                    }

                    if(array[1].trim()==''){
                        return
                    }

                    if(array[2].trim()==''){
                        return
                    }

                    time1 = array[1]
                    time2 = array[2]

                    var date1 = new Date(time1);
                    var date2 = new Date(time2);
                    var diffInMilliseconds = date2.getTime() - date1.getTime();
                    var diffInDays = Math.floor(diffInMilliseconds / (1000 * 60 * 60 * 24));
                    console.log(array[0] + '相差天数：' + diffInDays);
                    if (diffInDays >= 1 && diffInDays < 3) {

                        result1 = result1 + array[0].trim() + ","
                        num1 = num1 + 1
                    }

                    if (diffInDays >= 3 && diffInDays < 7) {
                        result2 = result2 + array[0].trim() + ","
                        num2 = num2 + 1
                    }

                    if (diffInDays >= 7) {
                        result3 = result3 + array[0].trim() + ","
                        num3 = num3 + 1
                    }

                }
            }
            result1 = result1.substring(0, result1.length - 1);
            result2 = result2.substring(0, result2.length - 1);
            result3 = result3.substring(0, result3.length - 1);


            var strf = '1、今天掉线的车辆请留意：'
            if (result1 != '') {
                strf = strf + '（' + result1 + '）1-2天未上线；'
            }

            if (result2 != '') {
                strf = strf + '（' + result2 + '）3天以上未上线；'
            }
            if (result3 != '') {
                strf = strf + '（' + result3 + '）7天以上未上线，'
            }
            strf = strf + '请及时留意车辆运作情况进行核实，避免车辆脱离监控。'

            document.getElementById("strf").value = strf
            document.getElementById("inputinfo").value = ''
            online()

        }


        
        setInterval(function(){

            var cp1 = document.getElementById("cp1"); //
                cp1.style.display = "none";

                var cp2 = document.getElementById("cp2"); //
                cp2.style.display = "none";
                joint()
           
        }, 500);




        document.onkeydown = function (e) {
            var ev = document.all ? window.event : e;
            if (ev.keyCode == 13) {
                var cp1 = document.getElementById("cp1"); //
                cp1.style.display = "none";

                var cp2 = document.getElementById("cp2"); //
                cp2.style.display = "none";
                joint()
                
            }
        }


        function copyText(flag) {

            var select = ''
            if (flag === 1) {
                select = document.getElementById("OnlineSituation")
                var cp2 = document.getElementById("cp2"); //
                cp2.style.display = "none";
            }

            if (flag === 2) {
                select = document.getElementById("strf")
                var cp1 = document.getElementById("cp1"); //
                cp1.style.display = "none";

            }
            if (select.value === '') {
                return

            }


            select.select(); //选中textarea中的内容
            try {
                var successful = document.execCommand('copy'); //执行复制命令
                if (successful) {
                    console.log('成功复制！');
                    if (flag === 1) {
                        var cp1 = document.getElementById("cp1"); //
                        cp1.style.display = "block";
                    }

                    if (flag === 2) {
                        var cp2 = document.getElementById("cp2"); //
                        cp2.style.display = "block";
                    }

                } else {
                    console.log('无法复制！');
                }
            } catch (err) {
                console.error('无法复制！', err);
            }
        }


        function online() {


            var totalNum = document.getElementById("totalNum").value
            var num = parseInt(totalNum);
            var online = num - parseInt(num1) - parseInt(num2) - parseInt(num3)
            var onlineStr = '当日委托监控的（' + num + '）辆车辆中，共有（' + online + '）辆车当日在线；（' + num1 + '）辆车1-2天未上线、（' + num2 + '）辆车连续3天未上线、（' + num3 + '）辆车连续7天未上线。'

            document.getElementById("OnlineSituation").value = onlineStr
            document.getElementById("totalNum").value = ''


        }



    </script>

    <script src="../js/bootstrap.bundle.min.js"></script>
</body>

</html>